package com.loveprogrammer.pool2.demo1;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.DestroyMode;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.impl.DefaultPooledObject;

import java.util.UUID;

/**
 * @ClassName ConnectorPoolObjectFactory
 * @Description 对象工厂
 * @Author admin
 * @Date 2023/11/7 9:40
 * @Version 1.0
 */
public class ConnectorPoolObjectFactory extends BasePooledObjectFactory<Connector> {
    @Override
    public Connector create() throws Exception {
        Connector connector = new Connector(UUID.randomUUID().toString());
        connector.create();
        return connector;
    }

    @Override
    public PooledObject<Connector> wrap(Connector connector) {
        return new DefaultPooledObject<>(connector);
    }

    @Override
    public void destroyObject(PooledObject<Connector> p, DestroyMode destroyMode) throws Exception {
        Connector object = p.getObject();
        object.destroy();
        // super.destroyObject(p, destroyMode);
    }

    @Override
    public boolean validateObject(PooledObject<Connector> pooledObject) {
        Connector object = pooledObject.getObject();
        return object.isValid();
    }
}
